<?php

class EmailTemplatesController extends BaseController
{
	
	/**
	 * 邮件模板管理
	 */
	public function actionAdmin()
	{
		$model = new EmailTemplates('search');
		$model->unsetAttributes();
		if(isset($_GET['EmailTemplates']))
			$model->attributes=$_GET['EmailTemplates'];
			
		$this->render('admin',array('model'=>$model));
	}

	/**
	 * 创建邮件模板
	 */
	public function actionCreate()
	{
		$model = new EmailTemplates();
		if(isset($_POST['EmailTemplates'])){
			$model->attributes=$_POST['EmailTemplates'];
			if($model->save()){
				//成功跳转
				$this->redirect(array('admin'));
			}
		}
			
		$this->render('create',array('model'=>$model));
	}
	
	/**
	 * 修改邮件模板
	 */
	public function actionUpdate($id)
	{
		$model = EmailTemplates::model()->findByPk($id);
		if(isset($_POST['EmailTemplates'])){
			$model->attributes=$_POST['EmailTemplates'];
			if($model->save()){
				//成功跳转
				$this->redirect(array('admin'));
			}
		}
			
		$this->render('update',array('model'=>$model));
	}
	
	/**
	 * 删除邮件模板
	 */
	public function actionDelete(){
		//操作ID号格式处理
		if(isset($_GET['id'])){
			$id = $_GET['id'];
		}elseif(isset($_POST['ids'])){
			$id =$_POST['ids'];
		}else{
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
		}
		$id = is_array($id)?join(',',$id):(int)$id;
		//处理条件
		$criteria=new CDbCriteria;
		$criteria->addCondition("zee_id in ($id)");

		//执行操作
		if(EmailTemplates::model()->deleteAll($criteria)){
			//成功跳转
			$this->redirect(Yii::app()->request->urlReferrer);
		}
	}
	
	/**
	 * 发送邮件
	 */
	public function actionSent(){
		//操作ID号格式处理
		if(isset($_GET['id'])){
			$id = $_GET['id'];
		}elseif(isset($_POST['ids'])){
			$id =$_POST['ids'];
		}
		
		if(isset($_POST['zeee_id'])){
			if(EmailTemplates::model()->sentMail($_POST['zm_id'],$_POST['zeee_id'])){
				//成功跳转
				$this->redirect(array($this->pre_module_url.'/member/admin'));
			}
		}
		
		$id = is_array($id)?join(',',$id):(int)$id;
		//处理条件
		$criteria=new CDbCriteria;
		$criteria->addCondition("zm_id in ($id)");
		$members = Member::model()->findAll($criteria);
		$this->render('sent_mail',array('members'=>$members));
	}
	
}